package com.dmap.analysispoint.service.impl;

import com.dmap.analysispoint.bo.DateAndNumBo;
import com.dmap.analysispoint.bo.NameAndValueBo;
import com.dmap.analysispoint.controller.background.param.CountParam;
import com.dmap.analysispoint.dao.BurialPointAddPhoneMapper;
import com.dmap.analysispoint.dao.BurialPointDayPhoneMapper;
import com.dmap.analysispoint.dao.BurialPointHourPhoneMapper;
import com.dmap.analysispoint.dao.BurialPointMapper;
import com.dmap.analysispoint.po.BurialPointAddPhoneExample;
import com.dmap.analysispoint.po.BurialPointDayPhoneExample;
import com.dmap.analysispoint.po.BurialPointExample;
import com.dmap.analysispoint.po.BurialPointHourPhoneExample;
import com.dmap.analysispoint.service.PhoneInfoAnalysisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:11/05/2018
 * \* Description:
 * \*
 */
@Service
public class PhoneInfoAnalysisServiceImpl implements PhoneInfoAnalysisService {

    @Resource
    private BurialPointAddPhoneMapper burialPointAddPhoneMapper;

    @Resource
    private BurialPointHourPhoneMapper burialPointHourPhoneMapper;

    @Resource
    private BurialPointDayPhoneMapper burialPointDayPhoneMapper;

    @Resource
    private BurialPointMapper burialPointMapper;

    /**
     *统计新增手机数
     */
    @Override
    public Integer countAddPhoneNum(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countPhoneByExample(example);
    }

    /**
     *按小时分组统计新增手机数
     */
    @Override
    public List<DateAndNumBo> countAddPhoneList(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countPhoneListByExample(example);
    }

    /**
     *统计手机各个版本的新增手机数
     */
    @Override
    public List<NameAndValueBo> countPhoneVersionAddPhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countPhoneVersionByExample(example);
    }

    /**
     *统计App各个版本的新增手机数
     */
    @Override
    public List<NameAndValueBo> countAppVersionAddPhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countAppVersionByExample(example);
    }

    /**
     *统计设备固件各个版本的新增手机数
     */
    @Override
    public List<NameAndValueBo> countFirmwareVersionAddPhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countFirmwareVersionByExample(example);
    }

    /**
     *统计各个渠道的新增手机数
     */
    @Override
    public List<NameAndValueBo> countPlaceAddPhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointAddPhoneExample example = this.getParamExample(param);
        return burialPointAddPhoneMapper.countPlaceByExample(example);
    }

    /**
     *BurialPointAddPhone表查询时对查询条件进行处理
     */
    private BurialPointAddPhoneExample getParamExample(CountParam param) {
        BurialPointAddPhoneExample example = new BurialPointAddPhoneExample();
        BurialPointAddPhoneExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andCreateTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

    /**
     *按小时分组统计活跃手机数
     */
    @Override
    public List<DateAndNumBo> countActivePhoneListByHour(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointHourPhoneExample example = this.getHourParamExample(param);
        return burialPointHourPhoneMapper.countActivePhoneListByExample(example);
    }

    /**
     *统计活跃手机数(跨小时)
     */
    @Override
    public Integer countActivePhoneNumByHour(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointHourPhoneExample example = this.getHourParamExample(param);
        return burialPointHourPhoneMapper.countPhoneByExample(example);
    }

    /**
     *获取最小的时间戳
     */
    @Override
    public Long selectMinTime() {
        BurialPointHourPhoneExample example = new BurialPointHourPhoneExample();;
        return burialPointHourPhoneMapper.selectMinTime(example);
    }

    /**
     *BurialPointHourPhone表查询时对查询条件进行处理
     */
    private BurialPointHourPhoneExample getHourParamExample(CountParam param) {
        BurialPointHourPhoneExample example = new BurialPointHourPhoneExample();
        BurialPointHourPhoneExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }

    /**
     *统计活跃手机数(跨天)
     */
    @Override
    public Integer countActivePhoneNumByDay(CountParam param) {
        if (param == null) {
            return 0;
        }
        BurialPointDayPhoneExample example = this.getDayParamExample(param);
        return burialPointDayPhoneMapper.countPhoneByExample(example);
    }

    /**
     *统计手机各个版本的活跃手机数
     */
    @Override
    public List<NameAndValueBo> countPhoneVersionActivePhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointDayPhoneExample example = this.getDayParamExample(param);
        return burialPointDayPhoneMapper.countPhoneVersionByExample(example);
    }

    /**
     *统计App各个版本的活跃手机数
     */
    @Override
    public List<NameAndValueBo> countAppVersionActivePhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointDayPhoneExample example = this.getDayParamExample(param);
        return burialPointDayPhoneMapper.countAppVersionByExample(example);
    }

    /**
     *统计设备固件各个版本的活跃手机数
     */
    @Override
    public List<NameAndValueBo> countFirmwareVersionActivePhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointDayPhoneExample example = this.getDayParamExample(param);
        return burialPointDayPhoneMapper.countFirmwareVersionByExample(example);
    }

    /**
     *统计各个渠道的活跃手机数
     */
    @Override
    public List<NameAndValueBo> countPlaceActivePhoneNum(CountParam param) {
        if (param == null) {
            return Collections.emptyList();
        }
        BurialPointDayPhoneExample example = this.getDayParamExample(param);
        return burialPointDayPhoneMapper.countPlaceByExample(example);
    }

    /**
     *BurialPointDayPhone表查询时对查询条件进行处理
     */
    private BurialPointDayPhoneExample getDayParamExample(CountParam param) {
        BurialPointDayPhoneExample example = new BurialPointDayPhoneExample();
        BurialPointDayPhoneExample.Criteria criteria = example.createCriteria();
        if (param.getCompanyId() != null){
            criteria.andCompanyIdEqualTo(param.getCompanyId());
        }
        if (!StringUtils.isBlank(param.getProjectId())){
            criteria.andProjectIdEqualTo(param.getProjectId());
        }
        if (param.getPlaceId() != null){
            criteria.andPlaceIdEqualTo(param.getPlaceId());
        }
        if (!StringUtils.isBlank(param.getAppVersion())) {
            criteria.andAppVersionEqualTo(param.getAppVersion());
        }
        if (!StringUtils.isBlank(param.getFirmwareId())) {
            criteria.andFirmwareIdEqualTo(param.getFirmwareId());
        }
        if (!StringUtils.isBlank(param.getFirmwareVersion())) {
            criteria.andFirmwareVersionEqualTo(param.getFirmwareVersion());
        }
        if (param.getStartTime() != null && param.getEndTime() != null && param.getEndTime() != 0) {
            criteria.andLoginTimeBetween(param.getStartTime(), param.getEndTime());
        }
        return example;
    }


    @Override
    public Integer countActivePhoneNumByBurialPoint(String projectId, Long startTime, Long endTime) {
        if (startTime == null || endTime == null || endTime == 0) {
            return 0;
        }
        BurialPointExample example =this.getBurialPointParamExample(projectId, startTime, endTime);
        return burialPointMapper.selectActivePhoneNumByProjectId(example);
    }

    /**
     *BurialPoint表查询时对查询条件进行处理
     */
    private BurialPointExample getBurialPointParamExample(String projectId, Long startTime, Long endTime) {
        BurialPointExample example = new BurialPointExample();
        BurialPointExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isBlank(projectId)){
            criteria.andProjectIdEqualTo(projectId);
        }
        if (startTime != null && endTime != null && endTime != 0) {
            criteria.andLoginTimeBetween(startTime, endTime);
        }
        return example;
    }
}
